package com.flying.invest.stat.service;

import java.util.Date;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.context.ServiceContext;
import com.flying.framework.service.AbstractService;
import com.flying.invest.stat.model.WaveStrategyStageStat;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;

@Service(value="StrategyStageStatService", desc="阶段策略统计")
public class WaveStrategyStageStatService extends AbstractService{
        
	@MethodInfo("新增")
	@DaoCreate(entity=WaveStrategyStageStat.ENTITY_NAME)
	public WaveStrategyStageStat createForBuy(
			@Param(value=WaveStrategyStageStat.USER_ID, required=true,	desc="所属用户") Long user_id,
			@Param(value=WaveStrategyStageStat.INVEST_ID, required=true,	desc="投资项目") Long invest_id,
			@Param(value=WaveStrategyStageStat.TYPE_ID, required=true,	desc="策略类型") Long type_id,
			@Param(value=WaveStrategyStageStat.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=WaveStrategyStageStat.DETAIL_ID, required=true,	desc="明细ID") Long detail_id,
			@Param(value=WaveStrategyStageStat.BUY_PRICE, required=true,	desc="买入价格") Double buy_price,
			@Param(value=WaveStrategyStageStat.BUY_AMOUNT, required=true,	desc="买入金额") Double buy_amount,
			@Param(value=WaveStrategyStageStat.BUY_QUANTITY, required=false,	desc="买入数量") Double buy_quantity,
			@Param(value=WaveStrategyStageStat.BUY_TRANSACTION_ID, required=true,	desc="买入交易ID") Long buy_transaction_id,
			@Param(value=WaveStrategyStageStat.PLAN_SELL_PRICE, required=false,	desc="计划卖出价格") Double plan_sell_price,
			@Param(value=WaveStrategyStageStat.BUY_TIME, required=false,	desc="买入时间") Date buy_time
		) {
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=WaveStrategyStageStat.ENTITY_NAME)
	public ActionResult updateForSell(
			@Param(value=WaveStrategyStageStat.STAT_ID, required=true,	desc="ID") Long stat_id,
			@Param(value=WaveStrategyStageStat.SELL_PRICE, required=false,	desc="卖出价格") Double sell_price,
			@Param(value=WaveStrategyStageStat.SELL_AMOUNT, required=false,	desc="卖出金额") Double sell_amount,
			@Param(value=WaveStrategyStageStat.SELL_QUANTITY, required=false,	desc="卖出数量") Double sell_quantity,
			@Param(value=WaveStrategyStageStat.SELL_TRANSACTION_ID, required=false,	desc="卖出交易ID") Long sell_transaction_id,
			@Param(value=WaveStrategyStageStat.SELL_TIME, required=false,	desc="卖出时间") Date sell_time,
			@Param(value=WaveStrategyStageStat.PROFIT, required=false,	desc="利差") Double profit ) {
		return null;
	}

	@MethodInfo("修改")
	@DaoUpdate(entity=WaveStrategyStageStat.ENTITY_NAME, wsql="sell_amount is null", maxEffectRows=1)
	public ActionResult updatePlanSellPriceByDetailId(
			@Param(value=WaveStrategyStageStat.DETAIL_ID, required=true,	desc="明细ID", tag="=") Long detail_id,
			@Param(value=WaveStrategyStageStat.PLAN_SELL_PRICE, required=false,	desc="计划卖出价格") Double plan_sell_price) {
		return null;
	}

	@MethodInfo("删除")
	@DaoRemove(entity=WaveStrategyStageStat.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=WaveStrategyStageStat.STAT_ID, required=true,	desc="ID") Long stat_id
		) {
		return null;
	}

	@SuppressWarnings("unchecked")
	@MethodInfo("搜索")
	@DaoQuery(entity=WaveStrategyStageStat.ENTITY_NAME, pageable=true, modelClass=WaveStrategyStageStat.class, 
		wsql="t.invest_id in(select invest_id from i_investment ii2 where ii2.status in(1,2))", 
		osql="order by sell_time, buy_time desc")
	public QueryResult<WaveStrategyStageStat> findAll(
			@Param(value=WaveStrategyStageStat.USER_ID, required=false,	desc="所属用户") Long user_id,
			@Param(value=WaveStrategyStageStat.INVEST_ID, required=false,	desc="投资项目") Long invest_id,
			@Param(value=WaveStrategyStageStat.TYPE_ID, required=false,	desc="策略类型") Long type_id,
			@Param(value=WaveStrategyStageStat.STRATEGY_ID, required=false,	desc="策略ID") Long strategy_id,
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		if(user_id == null) {
			var p = ServiceContext.getContext().getPrincipal();
			user_id = Long.parseLong(p.getId());
		}
		return ModelFactory.create(QueryResult.class, WaveStrategyStageStat.USER_ID, user_id);
	}

	@MethodInfo("查询")
	@DaoQuery(entity=WaveStrategyStageStat.ENTITY_NAME, single=true, throwsNotFoundException=true, wsql="sell_transaction_id is null" )
	public WaveStrategyStageStat findForSell(
			@Param(value=WaveStrategyStageStat.STRATEGY_ID, required=true,	desc="策略ID") Long strategy_id,
			@Param(value=WaveStrategyStageStat.DETAIL_ID, required=true,	desc="明细ID") Long detail_id
		) {
		return null;
	}

}